package com.work08.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.work08.common.R;
import com.work08.pojo.Comment;
import com.work08.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 绛河
 * @date 2023/6/19
 * @apiNote
 */
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/getAllById")
    private R<List<Comment>> getAllById(Integer articleId){
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getArticleId,articleId);
        List<Comment> list = commentService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    @GetMapping("/getAll")
    private R<List<Comment>> getAll(){
        List<Comment> list = commentService.list();
        return R.success(list);
    }

    @PostMapping("/addComment")
    private R<String> addComment(@RequestBody Comment comment){
        comment.setCreateTime(LocalDateTime.now());
        commentService.save(comment);
        return R.success("添加成功");
    }
}
